import * as types from '../constants/ActionTypes';
import Request from '../utils/request';
/**
 * 在线沟通页面，获取汽车信息
 */

const fetchCarInfo = (uid, cb) => (dispatch) => {
  Request.get({
    url: '/dealer/communicate?app=1',
    data: {
      uid,
    },
    success: (carInfo) => {
      console.log('success-carInfo', carInfo);
      if (carInfo && carInfo.utype) {
        dispatch(getCarInfo({}));
        if (cb) {
          cb({
            err: '参数不全或者该用户不在线，请退出沟通页面重试！',
          });
        }
      } else {
        dispatch(getCarInfo(carInfo));
      }
    },
    fail: () => {
      console.log('fail');
      dispatch(getCarInfo({}));
      if (cb) {
        cb({
          err: '获取数据失败！请退出沟通页面重试！',
        });
      }
    },
    error: () => {
      console.log('error');
      dispatch(getCarInfo({}));
      if (cb) {
        cb({
          err: '获取数据失败！请退出沟通页面重试！',
        });
      }
    },
  });
};

// 品牌
const fetchCarBrands = cb => (dispatch) => {
  Request.get({
    url: '/api/cars/brands',
    success: (carBrands) => {
      dispatch(getCarBrands(carBrands));
      if (cb && carBrands && carBrands.length > 0) {
        cb({
          result: carBrands[0].children,
        });
      }
    },
    fail: () => {
      console.log('fail');
      dispatch(getCarBrands({}));
      if (cb) {
        cb({
          err: '请求失败',
        });
      }
    },
    error: () => {
      console.log('error');
      dispatch(getCarBrands({}));
      if (cb) {
        cb({
          err: '服务器开小差了，请稍后再试',
        });
      }
    },
  });
};

// 车型接口
const fetchCarModels = id => (dispatch) => {
  console.log('carModelsid: ', id);
  Request.get({
    url: '/api/cars/models',
    data: {
      factory: id,
    },
    success: (carModels) => {
      dispatch(getCarModels(carModels));
    },
    fail: () => {
      console.log('fail');
      dispatch(getCarModels({}));
    },
    error: () => {
      console.log('error');
      dispatch(getCarModels({}));
    },
  });
};

// 款式
const fetchCarStyles = id => (dispatch) => {
  Request.get({
    url: '/api/cars/models/specs',
    data: {
      sid: id,
    },
    success: (carStyles) => {
      dispatch(getCarStyles(carStyles));
    },
    fail: () => {
      console.log('fail');
      dispatch(getCarStyles({}));
    },
    error: () => {
      console.log('error');
      dispatch(getCarStyles({}));
    },
  });
};

const fetchCompareCars = ids => (dispatch) => {
  Request.get({
    url: `/api/cars/specs/compare?ver=2&json=1&ids=${ids}`,
    success: (compare) => {
      dispatch(getCompareCars(compare));
    },
    fail: () => {
      console.log('fail');
      dispatch(getCompareCars({}));
    },
    error: () => {
      console.log('error');
      dispatch(getCompareCars({}));
    },
  });
};

const submitDriveBill = (driveBill, cb) => () => {
  Request.post({
    url: '/dealer/testdrive',
    contenttype: 'form',
    data: driveBill,
    success: (data) => {
      if (cb) {
        cb(data);
      }
    },
    fail: () => {
      if (cb) {
        cb({ message: '发送表单失败，请稍后再试！' });
      }
    },
    error: () => {
      if (cb) {
        cb({ message: '网络有误，请稍后再试！' });
      }
    },
  });
};

const submitPriceBill = (priceBill, cb) => () => {
  console.log('参数', priceBill);
  Request.post({
    url: '/dealer/bill',
    contenttype: 'form',
    data: priceBill,
    success: (data) => {
      if (cb) {
        cb(data);
      }
    },
    fail: () => {
      if (cb) {
        cb({ message: '发送表单失败，请稍后再试！' });
      }
    },
    error: () => {
      if (cb) {
        cb({ message: '网络有误，请稍后再试！' });
      }
    },
  });
};

const fetchLockUser = (uid, cb) => () => {
  Request.get({
    url: '/dealer/lockuser',
    data: {
      uid,
    },
    success: (data) => {
      if (cb) {
        cb(data);
      }
    },
    fail: () => {
      if (cb) {
        cb({ err: '通话失败该客户已经被其他人接通，连接失败。' });
      }
    },
    error: () => {
      if (cb) {
        cb({ err: '通话失败该客户已经被其他人接通，连接失败。' });
      }
    },
  });
};

const getCarInfo = carInfo => ({
  type: types.CAR_INFO,
  carInfo,
});

// const postDriveBill = driveBill => ({
//   type: types.DRIVE_BILL,
//   driveBill,
// });

// const postPriceBill = priceBill => ({
//   type: types.PRICE_BILL,
//   priceBill,
// });

const getCarBrands = carBrands => ({
  type: types.CAR_BRAND,
  carBrands,
});

const changeSubBrand = subBrand => ({
  type: types.SUB_BRAND,
  subBrand,
});

const getCarModels = carModels => ({
  type: types.CAR_MODEL,
  carModels,
});

const changeCarModel = carModel => ({
  type: types.MODEL,
  carModel,
});

const getCarStyles = carStyles => ({
  type: types.CAR_STYLE,
  carStyles,
});

const changeCarStyle = carStyle => ({
  type: types.STYLE,
  carStyle,
});

const getCompareCars = compare => ({
  type: types.COMPARE_CAR,
  compare,
});

const getRecommendCar = recommendCar => ({
  type: types.RECOMMEND_CAR,
  recommendCar,
});

const selectingCompareCar = compareCar => ({
  type: types.SELECT_COMPARE_CAR,
  compareCar,
});

const editPriceMsg = (name, phone, sex, address, counselor) => ({
  type: types.MODIFY_PRICE,
  name,
  phone,
  sex,
  address,
  counselor,
});

export {
  fetchCarInfo,
  submitDriveBill,
  submitPriceBill,
  fetchCarBrands,
  changeSubBrand,
  fetchCarModels,
  changeCarModel,
  fetchCarStyles,
  changeCarStyle,
  fetchCompareCars,
  getRecommendCar,
  selectingCompareCar,
  editPriceMsg,
  fetchLockUser,
};
